package com.aptech.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import com.aptech.model.Userprofile;
import com.aptech.session.GetSessionFactory;
import com.opensymphony.xwork2.ActionSupport;

public class LoginDao extends ActionSupport{
	
	
	//连接数据库，根据用户名验证登录
	public String userLogin(Userprofile user){
		Session  session=GetSessionFactory.getInstance().getSessionFactory().openSession();
		String sql="select password from UserProfile  where username=?";
		try{
			Query q=session.createSQLQuery(sql);
			q.setParameter(0,user.getUsername());
			Object o=q.uniqueResult();
			//当返回的对象O为null时，说明用户不存在
			if(o==null){
				//用户不存在，返回notUser
				return "notUser";
			}else{
				String password=o.toString();
				if(password.equals(user.getPassword())){
					//密码正确返回SUCCESS
					return SUCCESS;
				}else{
					//密码错误，返回passWordError
					return "passwordError";
				}
			}
		}finally{
			if(session!=null){
				session.close();
			}
		}
	}
	
	//查询用户级别信息
		public String selectLv(Userprofile user){
			Session session=GetSessionFactory.getInstance().getSessionFactory().openSession();
			String sql="select lv from UserProfile where username=?";
			try{
				Query query=session.createSQLQuery(sql);
				query.setParameter(0,user.getUsername());
				Object o=query.uniqueResult();
				return o.toString();
			}finally{
				if(session!=null){
					session.close();
				}
			}
		}
}
